<?php
namespace app\ask\controller;

use think\Controller;

class Center extends Controller
{
    //关联查询的意思
    protected $relationSearch = true;
    public function __construct()
    {
        parent::__construct();
        $this->PostModel=model("Post.Post");
        $this->CateModel=model("Post.Category");
        $this->BusinessModel=model("Business.Business");
        $this->CommentModel=model("Post.Comment");
        $this->CollectModel=model("Post.Collect");
    }

    // 查询用户信息
    public function business()
    {
        if($this->request->isPost())
        {
            $busid=$this->request->param('busid',0,'trim');
           
            $business=$this->BusinessModel->find($busid);
            
            if(!$business)
            {
                $this->error('用户不存在');
                exit;
            }else
            {
                $this->success('返回用户信息',null,$business);
                exit;
            }
        }
    }

    public function post()
    {
        if($this->request->isPost())
        {
            $busid=$this->request->param('busid',0,'trim');
            $keywords=$this->request->param('keywords','','trim');
            $page=$this->request->param('page',1,'trim');
            $limit=5;
            $start=($page-1)*$limit;
            $where=[];
            $business=$this->BusinessModel->find($busid);
            
            if(!$business)
            {
                $this->error('用户不存在');
                exit;
            }
            $where['busid']=$business['id'];
            if(!empty($keywords))
            {
                $where['title'] = ['LIKE',"%$keywords%"];
            }
            
            $post=$this->PostModel->with(['business','category'])->where($where)->limit($start,$limit)->select();
            $PostCount=$this->PostModel->where($where)->count();
            // array_diff()
            $data=[
                'post'=>$post,
                'PostCount'=>$PostCount,
            ];
            if(!$post)
            {
                $this->error('暂无帖子数据');
                exit;
                
            }else
            {
                $this->success('有帖子数据',null,$data);
                exit;
            }
        }
    }

    // 查询客户评论
    public function comment()
    {
        if($this->request->isPost())
        {
            $busid=$this->request->param('busid',0,'trim');
            $keywords=$this->request->param('keywords','','trim');
            $page=$this->request->param('page',1,'trim');
            $limit=5;
            $start=($page-1)*$limit;
            $where=[];
            $business=$this->BusinessModel->find($busid);
            if(!$business)
            {
                $this->error('用户不存在');
                exit;
            }
            $where['busid']=$business['id'];
            if(!empty($keywords))
            {
                $where['content'] = ['LIKE',"%$keywords%"];
            }
            
            $comment=$this->CommentModel->with(['business'])->where($where)->limit($start,$limit)->select();
            // echo $this->CommentModel->getLastSql();
            // exit;
            foreach($comment as $item)
            {
                $post=$this->PostModel->with(['business','category'])->find($item['postid']);
               
                $item['post']=$post;
                
            }
            // halt($comment);
            // exit;
            $CommentCount=$this->CommentModel->where($where)->count();
          
            $data=[
                'comment'=>$comment,
                'CommentCount'=>$CommentCount,
            ];
            // halt($comment);
            // exit;
            if(!$comment)
            {
                $this->error('暂无评论数据');
                exit;
            }else
            {
                $this->success('有评论数据',null,$data);
                exit;
            }
        }
    }

    // 查询收藏
    public function collect()
    {
        if($this->request->isPost())
        {
            $busid=$this->request->param('busid',0,'trim');
            $keywords=$this->request->param('keywords','','trim');
            $page=$this->request->param('page',1,'trim');
            $limit=5;
            $start=($page-1)*$limit;
            // var_dump($busid);
            // exit;
            $business=$this->BusinessModel->find($busid);
            if(!$business)
            {
                $this->error('用户不存在');
                exit;
            }
            $where=[
                'collect.busid'=>$business['id']
            ];
            if(!empty($keywords))
            {
                $where['title'] = ['LIKE',"%$keywords%"];
            }

            $collect=$this->CollectModel->with('post')->where($where)->limit($start,$limit)->select();
            // halt(collection($collect)->toArray());
            
            foreach($collect as $item)
            {
                $post=$this->PostModel->with(['business','category'])->find($item['postid']);
                $item['business']=$post['business'];
                $item['category']=$post['category'];
                // var_dump($item);
            }
            // halt(collection($collect)->toArray());
            // exit;
            // halt($where);
            $CollectCount=$this->CollectModel->with('post')->where($where)->count();
          
            $data=[
                'collect'=>$collect,
                'CollectCount'=>$CollectCount,
            ];
            if(!$collect)
            {
                $this->error('暂无收藏数据');
                exit;
            }else
            {
                $this->success('有收藏数据',null,$data);
                exit;
            }
        }
    }

   
}